import random

import pymysql
from common import snowFaker as sf
from faker import Faker

from python_faker.faker09 import getCanteenIdList

# 初始化Faker实例
fake = Faker('zh_CN')
def insert_flavor_data(flavor,canteenId):
    # 连接数据库
    conn = pymysql.connect(host="localhost", user="root", password="123456", database="esl", charset="utf8mb4")
    cursor = conn.cursor()

    try:
        id = sf.get_id()
        flavor_id = fake.uuid4().replace('-', '')
        flavor = flavor
        price = random.randint(1, 5)  # 随机生成价格
        data = (id,flavor_id,flavor,price,canteenId)
        # 插入数据
        query = """
        INSERT INTO flavor (id,flavor_id, flavor, flavor_price, canteen_id)
        VALUES (%s, %s, %s, %s, %s)
        """
        cursor.execute(query, data)
        conn.commit()  # 提交事务

        print("数据插入成功！")

    except Exception as e:
        print(f"插入数据时出错: {e}")
        conn.rollback()  # 出现错误时回滚事务

    finally:
        # 关闭连接
        cursor.close()
        conn.close()

if __name__ == "__main__":
    rows = getCanteenIdList()
    for index,row in enumerate(rows):
        data = {'酸': ('微酸', '特酸', '变态酸'), '甜': ('微甜', '特甜', '变态甜'), '苦': ('微苦', '特苦', '变态苦'),
                '辣': ('微辣', '特辣', '变态辣'), '咸': ('微咸', '特咸', '变态咸')}
        for key in data.keys():
            values = data[key]
            for value in values:
                insert_flavor_data(value,row[0])
